using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Controls;
using Converters;
using ProjectView.ProjectControls;
using ProjectView.RuleView;
using SExperiment;
using SProject;

namespace MainView.MainControls
{
	public partial class UCSubsetParamter : UserControl, IComponentConnector
	{
		public static readonly DependencyProperty SubsetParamterTypeProperty = DependencyProperty.Register("SubsetParamterType", typeof(ESubsetParamterType), typeof(UCSubsetParamter), new UIPropertyMetadata(ESubsetParamterType.BasicOption));

		public static readonly DependencyProperty SubsetProperty = DependencyProperty.Register("Subset", typeof(Subset), typeof(UCSubsetParamter), new UIPropertyMetadata(null, OnSubsetChanged));

		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(UCSubsetParamter), new UIPropertyMetadata(true));

		private TabControlEx tabControl = new TabControlEx();

		public ESubsetParamterType SubsetParamterType
		{
			get
			{
				return (ESubsetParamterType)GetValue(SubsetParamterTypeProperty);
			}
			set
			{
				SetValue(SubsetParamterTypeProperty, value);
			}
		}

		public Subset Subset
		{
			get
			{
				return (Subset)GetValue(SubsetProperty);
			}
			set
			{
				SetValue(SubsetProperty, value);
			}
		}

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		private static void OnSubsetChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCSubsetParamter uCSubsetParamter = sender as UCSubsetParamter;
			if (uCSubsetParamter != null)
			{
				uCSubsetParamter.RefreshSubsetProjectBasicOption();
			}
		}

		public UCSubsetParamter()
		{
			InitializeComponent();
			tabControl.SetResourceReference(FrameworkElement.StyleProperty, "TabControlStylesmall");
		}

		public void RefreshFirstBasicOption(Project prj)
		{
			if (prj == null)
			{
				return;
			}
			foreach (TabItem item in (IEnumerable)tabControl.Items)
			{
				if (item.Header.ToString() == prj.BasicInfo.Name)
				{
					item.IsSelected = true;
					break;
				}
			}
		}

		private void SetControlIsReadOnly(UserControl uc, DependencyProperty IsReadOnlyProperty, Subset.SubsetParamter sp)
		{
			MultiBinding multiBinding = new MultiBinding();
			Binding binding = new Binding("IsDecrypt");
			binding.Source = this;
			Binding binding2 = binding;
			binding2.Converter = new BoolReverseConverter();
			multiBinding.Bindings.Add(binding2);
			Binding binding3 = new Binding("IsReadOnly");
			binding3.Source = sp.BasicOption;
			binding2 = binding3;
			multiBinding.Bindings.Add(binding2);
			multiBinding.Converter = new BoolOrConverter();
			uc.SetBinding(IsReadOnlyProperty, multiBinding);
		}

		public void RefreshSubsetProjectBasicOption()
		{
			grid.Children.Clear();
			tabControl.Items.Clear();
			tabControl.Visibility = Visibility.Hidden;
			if (Subset == null)
			{
				return;
			}
			List<Well> wells = Subset.Wells;
			IList<Project> list = (from s in wells
				select s.Project into s
				where s != null
				select s).Distinct().ToList();
			if (list.Count > 1)
			{
				grid.Children.Add(tabControl);
				tabControl.Visibility = Visibility.Visible;
			}
			foreach (Project item in list)
			{
				Subset.SubsetParamter subsetParamter = Subset.GetSubsetParamter(item);
				if (subsetParamter != null)
				{
					UserControl userControl = null;
					switch (SubsetParamterType)
					{
					default:
						return;
					case ESubsetParamterType.BasicOption:
						userControl = new UCProjectBasicOption();
						userControl.SetBinding(UCProjectBasicOption.BasicOptionProperty, new Binding("BasicOption")
						{
							Source = subsetParamter
						});
						SetControlIsReadOnly(userControl, UCProjectBasicOption.IsReadOnlyProperty, subsetParamter);
						break;
					case ESubsetParamterType.MeltingOption:
					{
						UCMeltingOption uCMeltingOption = new UCMeltingOption();
						uCMeltingOption.Project = item;
						userControl = uCMeltingOption;
						userControl.SetBinding(FrameworkElement.DataContextProperty, new Binding("BasicOption")
						{
							Source = subsetParamter
						});
						SetControlIsReadOnly(userControl, UCMeltingOption.IsReadOnlyProperty, subsetParamter);
						break;
					}
					case ESubsetParamterType.HRMOption:
					{
						UCHRMOption uCHRMOption = new UCHRMOption();
						uCHRMOption.Project = item;
						userControl = uCHRMOption;
						userControl.SetBinding(FrameworkElement.DataContextProperty, new Binding("BasicOption")
						{
							Source = subsetParamter
						});
						SetControlIsReadOnly(userControl, UCHRMOption.IsReadOnlyProperty, subsetParamter);
						break;
					}
					case ESubsetParamterType.ADOption:
						userControl = new UCADOption();
						userControl.SetBinding(FrameworkElement.DataContextProperty, new Binding("BasicOption")
						{
							Source = subsetParamter
						});
						SetControlIsReadOnly(userControl, UCADOption.IsReadOnlyProperty, subsetParamter);
						break;
					case ESubsetParamterType.AnalysisParamter:
						userControl = new UCAnalysisParamterOption();
						userControl.SetBinding(UCAnalysisParamterOption.BasicOptionProperty, new Binding("BasicOption")
						{
							Source = subsetParamter
						});
						SetControlIsReadOnly(userControl, UCAnalysisParamterOption.IsReadOnlyProperty, subsetParamter);
						break;
					case ESubsetParamterType.Detection:
					case ESubsetParamterType.CrossTalk:
					case ESubsetParamterType.Rule:
						return;
					}
					if (list.Count == 1)
					{
						grid.Children.Add(userControl);
						continue;
					}
					TabItem tabItem = new TabItem();
					tabItem.Content = userControl;
					tabItem.Header = item.BasicInfo.Name;
					tabControl.Items.Add(tabItem);
				}
			}
			if (tabControl.Items.Count > 0)
			{
				tabControl.SelectedIndex = 0;
			}
			tabControl.RefreshTabItemStyle();
		}

		public bool Validate()
		{
			if (tabControl.Visibility == Visibility.Visible)
			{
				foreach (TabItem item in (IEnumerable)tabControl.Items)
				{
					if (!ValidateControl(item.Content))
					{
						item.IsSelected = true;
						return false;
					}
				}
			}
			else if (grid.Children.Count > 0 && !ValidateControl(grid.Children[grid.Children.Count - 1]))
			{
				return false;
			}
			return true;
		}

		private bool ValidateControl(object ctl)
		{
			if (ctl is UCProjectBasicOption)
			{
				int cycles = 0;
				if (Subset.GetAllProject().Count > 0)
				{
					cycles = Subset.GetAllProject()[0].PCRProgram.GetAcquisitionCycleCount();
				}
				if (!((UCProjectBasicOption)ctl).Validate(cycles))
				{
					return false;
				}
			}
			else if (ctl is UCMeltingOption)
			{
				if (!((UCMeltingOption)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCHRMOption)
			{
				if (!((UCHRMOption)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCADOption)
			{
				if (!((UCADOption)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCProjectDetection)
			{
				if (!((UCProjectDetection)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCProjectCrossTalk)
			{
				if (!((UCProjectCrossTalk)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCRule)
			{
				if (!((UCRule)ctl).Validate())
				{
					return false;
				}
			}
			else if (ctl is UCAnalysisParamterOption)
			{
				int cycles2 = 0;
				if (Subset.GetAllProject().Count > 0)
				{
					cycles2 = Subset.GetAllProject()[0].PCRProgram.GetAcquisitionCycleCount();
				}
				if (!((UCAnalysisParamterOption)ctl).Validate(cycles2))
				{
					return false;
				}
			}
			return true;
		}
	}
}
